<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <%@include file="/WEB-INF/jsp/common/common.jsp"%>
	<script>
	    function pageQuery(pageIndex) {
	    	
	    	$.ajax({
	    		type : "POST",
	    		url  : "${APP_PATH}/user/pageQuery.do",
	    		data : {
	    			pageno : pageIndex+1,
	    			pagesize : 5
	    		},
	    		beforeSend : function() {
	    			return true;
	    		},
	    		success : function(r) {
	    			if ( r.success ) {
	    				
	    				var page = r.obj;
	    				var datas = page.dataList;
	    				var totalsize = page.totalsize;
	    				var totalno = page.totalno;
	    				
	    				$("#totalsizeSpan").text(totalsize);
	    				$("#totalnoSpan").text(totalno);
	    				
	    				var content = "";
	    				$.each(datas, function(i, user){
		                    content += '<tr class="odd">';
		                    content += '  <td><input type="checkbox" value="'+user.id+'"/></td>';
		                    content += '  <td>'+(i+1)+'</td>';
		                    content += '  <td>'+user.loginacct+'</td>';
		                    content += '  <td>'+user.username+'</td>';
		                    content += '  <td>'+user.createtime+'</td>';
		                    content += '  </tr>';
	    				});
	    				
	    				$("#dataBody").html(content);
	    				
	    				//显示翻页
	    				/**/
						$("#pagination").pagination(totalsize, {//10总记录条数
							initquery:false,
		                    callback: pageQuery,//每次点击翻页按钮的时候会被自动调用，名字随意取 
		                    items_per_page:5, //每页显示多少条
		                    current_page:pageIndex,//当前页
		                    link_to:"javascript:void(0)",//不希望跳转到某个目的地，只执行回调函数
		                    num_display_entries:5,//也是几个页码
		                    next_text:"下一页",//下一页按钮上显示的文本
		                    next_show_always:true,//如果没有下一页  按钮仍然显示  用true；否则用false
		                    prev_text:"上一页",//上一页按钮上显示的文本
		                    prev_show_always:true,
		                    num_edge_entries:2,//页码较多的时候，省略部分
		                    ellipse_text:"..."
		                });
	    				
	    			} else {
	    				alert("数据查询失败");
	    			}
	    		}
	    	});
	    }
	
	    $(function(){
	        $("#insertBtn").click(function(){
	        	window.location.href = "${APP_PATH}/user/add.do";
	        });
	        
	        $("#updateBtn").click(function(){
	        	// 判断选择复选框的数量
	        	var users = $("input:checked", $("#dataBody"));
	        	
	        	if ( users.length == 0 ) {
	        		alert("必须选择一条数据进行修改");
	        	} else if ( users.length > 1 ) {
	        		alert("只能选择一条数据进行修改");
	        	} else {
	        		var user = users[0];
	        		window.location.href = "${APP_PATH}/user/edit.do?id="+$(user).val();
	        	}
	        });
	        
	        $("#deleteBtn").click(function(){
	        	var users = $("input:checked", $("#dataBody"));
	        	var ids = [];
	        	if ( users.length == 0 ) {
	        		alert("必须选择数据进行删除");
	        	} else {
	        		var flg = confirm("删除选择的用户信息，是否继续？");
	        		if ( flg ) {
	        			// 删除数据
	        			// name=value&name=value
	        			// obj[0].id=1&obj[1].id=2&obj[0].name=zhangsan
	        			var paramObj = {};
	        			
	        			$.each( users, function(i, n) {
	        				paramObj["users["+i+"].id"] = this.value;
	        				//paramObj.obj[i].id = this.value
	        			} );

	        			$.ajax({
	        				type : "POST",
	        				url  : "${APP_PATH}/user/deleteUsers.do",
	        				data : paramObj,
	        				beforeSend : function() {
	        					return true;
	        				},
	        				success : function( r ) {
	        					if ( r.success ) {
	        						alert("删除成功");
	        						pageQuery(0);
	        					} else {
	        						alert("删除失败");
	        					}
	        				}
	        			});
	        		}
	        	}
	        });
	        
	        // 使用ajax的方式异步查询数据
	        pageQuery(0);
	    });
	</script>
</head>

<body>
<table  border="0" cellpadding="0" cellspacing="0" class="table_border">
  <tr>
    <td><table width="100%" border="0" cellpadding="0" cellspacing="0" class="table_right">
      <tr>
        <td width="25" height="26" align="right"><img src="../images/ico_location.gif" width="16" height="15" /></td>
        <td><strong>位置：</strong>首页&gt;权限管理&gt;用户维护</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellspacing="0" class="table_padding">
      <tr>
        <td align="center">		
		<table  border="0" cellpadding="0" cellspacing="0" class="table_border">
          <tr>
            <td><table width="100%" border="0" cellpadding="0" cellspacing="0" class="table_right">
              <tr>
                <td height="26" align="right"><span class="font_right">
                  <input id="insertBtn" type="button" value="新增"/>
                  <input id="deleteBtn" type="button" value="删除"/>
                  <input id="updateBtn" type="button" value="修改"/>
                </span></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="122"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td align="center"><table border="0" cellpadding="0" cellspacing="0" id="box_num_table2" class="box_num_table">
                  <thead>
                    <tr>
                      <td width="8%"><input type="checkbox"/></td>
                      <td width="7%">序号</td>
                      <td width="12%">登录帐号</td>
                      <td width="11%">用户姓名</td>
                      <td width="12%">创建时间</td>
                      </tr>
                  </thead>
                  <tbody id="dataBody">
                     <%-- 
                     <!-- 
                     <c:forEach items="${page.dataList}" var="user" varStatus="status">
	                    <tr class="odd">
	                      <td><input type="checkbox"/></td>
	                      <td>${status.count}</td>
	                      <td>${user.loginacct}</td>
	                      <td >${user.username}</td>
	                      <td>${user.createtime}</td>
	                      </tr> 
                     </c:forEach>
                      -->
                      --%>
                  </tbody>
                </table></td>
              </tr>
              <tr>
                <td><table width="100%" height="30" border="0" cellpadding="0" cellspacing="0" class="page_table">
                  <tr>
                    <td width="8%" class="font_left">数据:<span id="totalsizeSpan"></span>条</td>
                    <td width="12" class="font_left">第</td>
                    <td width="375" class="font_left"><input name="textfield2" type="text" size="2" maxlength="4" />
                      /<span id="totalnoSpan"></span>页
                      <input type="image" border="0" align="absmiddle"  name="RedImg" src="../images/botton_page_to.png" onclick="this.form.submit()" /></td>
                    <td width="478" class="font_right"><div id="pagination"></div></td>
                  </tr>
                </table></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  </form>
</table>
</body>
</html>
